#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <string>
#include <sstream>

using namespace std;

void myprintf(vector<vector<int> >ans);

struct node{
	int num=0;
	vector<vector<int> >a;
};
int flag=0;
vector<int>reactants(21),products;
vector<node>equations(20);
vector<vector<int> >ans(21);


void dfs(vector<int>visit,int num,vector<vector<int> >tans){
	if(num==products.size()){
		myprintf(tans);
		printf("#####\n");
		if(flag==0){
			ans=tans;
			flag=1;
		}else{
			int flag2=0;
			for(int i=0;i<products.size();i++){
				int flag3=0;
				int index=products[i];
				for(int j=0;j<ans[index].size()&&j<tans[index].size();j++){
					if(tans[index][j]<ans[index][j]){
						flag3=1;
						flag2=1;
						break;
					}
				}
				if(flag3)break;
			}
			if(flag2)ans=tans;
		}
		return;
	}
	int index=products[num];
	for(int i=0;i<equations[index].num;i++){
		int flag=1;
		for(int j=0;j<equations[index].a[i].size();j++){
			int tem=equations[index].a[i][j];
			if(visit[tem]==1||reactants[tem]==0){
				flag=-1;
				break;
			}
		}
		if(flag==1){
			for(int j=0;j<equations[index].a[i].size();j++){
				int tem=equations[index].a[i][j];
				visit[tem]==1;
				tans[index].push_back(tem);
			}
			dfs(visit,num+1,tans);
			tans[index].clear();
		}
	}
}

int main(){
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		int index;
		scanf("%d",&index);
		reactants[index]=1;
		vector<int>tem(1);
		tem[0]=index;
		equations[index].a.push_back(tem);
		equations[index].num++;
	}
	
	int m;
	scanf("%d",&m);
	products.resize(m);
	int cnt=0;
	for(int i=0;i<m;i++){
		int index;
		scanf("%d",&index);
		products[cnt++]=index;
	}
	
	int k;
	scanf("%d",&k);
	getchar();
	for(int i=0;i<k;i++){
		string s;
		getline(cin,s,'>');
		stringstream ss(s);
		int reactant;
		int product;
		scanf("%d",&product);
		equations[product].num++;
		char c;
		vector<int>tem;
		while(ss>>reactant){
			tem.emplace_back(reactant);
			ss>>c;
		}
		equations[product].a.emplace_back(tem);
	}
	
	vector<vector<int> >tans(21);
	vector<int>visit(21);
	dfs(visit,0,tans);
	
	myprintf(ans); 
	

	return 0;
}

void myprintf(vector<vector<int> >myans){
	for(int i=0;i<products.size();i++){
		int index=products[i];
		for(int j=0;j<myans[index].size();j++){
			printf("%02d",myans[index][j]);
			if(j!=myans[index].size()-1){
				printf(" + ");
			} else{
				printf(" -> ");
			}
		}
		printf("%02d\n",index);
	}
}


